<?php
namespace app\api\controller;
use think\Controller;
use think\Request;
use think\Db;
class Publics 
{
    public $return_data=['Code'=>1,'Msg'=>'','ErrorSql'=>'','Data'=>[]];
    // 分类
    public function category()
    {
           $p = Controller('Publics', 'logic');
           $res = $p->category();
           return json($res)->code(200)->header(['Content-Type' => 'application/json']);
    }
    //用户 信息 
    public function userInfo($input)
    {
          $user = Controller('Publics', 'logic');
          $res = $user->userInfo($input);
          return $this->print_result($res);

    }
    // 上传图片-tmp
    public function uploadImg1(){
        $cover = isset($_FILES['cover']) ? $_FILES['cover'] : '';
        if(!$cover){
            $this->res_data['Msg']="上传错误1";
            return $this->print_result($this->res_data);
        }
        $pic_content = get_img_content($cover);
        $filename='goods_cover_'.uniqid()."_".mt_rand(10000,99999).'.png';
        $datepath = date('Ymd');
        $filepath = '/upload/cover/'.date('Ymd').'/'.$filename;
        $basepath = config('cover_folder').date('Ymd');
        if(!is_dir($basepath)){
          mkdir($basepath);
        }
        $file=$basepath.'/'.$filename;
        if (!file_put_contents($file, $pic_content)){
           $this->res_data['Msg']='服务器繁忙';
           return $this->res_data;
        }
        $c['cover'] = get_base_img($filepath);
        $c['path'] = $filepath;
        $this->res_data['Code']=0;
        $this->res_data['Msg']='成功';
        $this->res_data['Data']=$c;// 商品封面
        return $this->print_result($this->res_data);   
  
    }
    //上传 封面
    public function uploadImg(){
        $cover = isset($_FILES['cover']) ? $_FILES['cover'] : '';
        if(!$cover){
            $this->res_data['Msg']="上传错误";
            return $this->print_result($this->res_data);
        }
        $maxSize = 10 * 1024 * 1024;
        if ($cover['size'] > $maxSize) {
            $this->res_data['Msg']="上传错误";
            return $this->print_result($this->res_data);
        }
        $filename='goods_cover_'.uniqid()."_".mt_rand(10000,99999).'.png';
        $datepath = date('Ymd');
        $filepath = '/upload/cover/'.date('Ymd').'/'.$filename;
        $basepath = config('cover_folder').date('Ymd');
        if(!is_dir($basepath)){
          mkdir($basepath);
        }
        $file=$basepath.'/'.$filename;
        if (!move_uploaded_file($cover['tmp_name'], $file)){
            $this->res_data['Msg']="上传错误";
            return $this->print_result($this->res_data);
        }
        $c['cover'] = get_base_img($filepath);
        $c['path'] = $filepath;
        $this->res_data['Code']=0;
        $this->res_data['Msg']='成功';
        $this->res_data['Data']=$c;// 商品封面
        return $this->print_result($this->res_data);   
    }
    //上传视频
    public function uploadVideo(){
        $v = isset($_FILES['v']) ? $_FILES['v'] : '';
        if(!$v){
            $this->res_data['Msg']="上传错误";
            return $this->print_result($this->res_data);
        }
        $maxSize = 10 * 1024 * 1024;
        if ($v['size'] > $maxSize) {
            $this->res_data['Msg']="上传错误";
            return $this->print_result($this->res_data);
        }
        $filename='goods_cover_'.uniqid()."_".mt_rand(10000,99999).'.mp4';
        $datepath = date('Ymd');
        $filepath = '/upload/cover/'.date('Ymd').'/'.$filename;
        $basepath = config('cover_folder').date('Ymd');
        if(!is_dir($basepath)){
          mkdir($basepath);
        }
        $file=$basepath.'/'.$filename;
        if (!move_uploaded_file($v['tmp_name'], $file)){
            $this->res_data['Msg']="上传错误";
            return $this->print_result($this->res_data);
        }
        $c['video'] = get_base_img($filepath);
        $c['path'] = $filepath;
        $this->res_data['Code']=0;
        $this->res_data['Msg']='成功';
        $this->res_data['Data']=$c;// 商品封面
        return $this->print_result($this->res_data);   
    }
    public function print_result($return_data){
        if(!is_array($return_data)){
            $return_data['Code']=0;
            $return_data['Msg']='输出信息不符合规范';
        }
        return json($return_data)->code(200)->header(['Content-Type' => 'application/json']);
    }


    // 上课通知
    public function class_notice()
    {
        $template_id = 'u2f1bRAR7xLy7Bj1GwSdGrR9wzaw0tV4gS2kGPXcdcg';
        $PublicsLogic = Controller('Publics', 'logic');
        $send_class_notice_time = [];
        $system = Db::name('system')->field('sid,send_class_notice_time')->select();
        if ($system) {
            foreach ($system as $sys) {
                $send_class_notice_time[$sys['sid']] = $sys['send_class_notice_time'];
            }
        }
        $classtime = Db::name('classtime')->where(['status'=>1])->select();
        $today_start = strtotime(date('Y-m-d'));
        $today_end = $today_start+86400;
        if ($classtime) {
            foreach ($classtime as $key => $ct) {
                $course = Db::name('course')->where('id',$ct['course_id'])->find();
                $notice_time = 0;
                if (isset($send_class_notice_time[$course['sid']])) {
                    $notice_time = $send_class_notice_time[$course['sid']]*60;
                }
                if ($ct['begin_time']<time()+$notice_time && $ct['begin_time']>$today_start) {
                    $students = Db::name('user_course')->alias('uc')->field('u.name,u.fwhopenid,u.id,u.sid')->leftJoin('user u','u.id=uc.user_id')->where([['uc.course_id','=',$course['id']],['uc.status','in',[1,2]]])->select();
                    if ($students) {
                        foreach ($students as $k => $stu) {
                            if (Db::name('tuisong_recode')->where([['user_id','=',$stu['id']],['classtime_id','=',$ct['id']],['create_time','>',$today_start],['create_time','<',$today_end]])->find()) {
                                continue;
                            }
                            if (isset($stu['fwhopenid']) && $stu['fwhopenid']) {
                                $page = 'orderPackages/pages/myChild/myClassTime?notice=1&sid='.$stu['sid'];
                                $data = [
                                    'first' => ['value' => '你好，以下课程将在'.($notice_time/60).'分钟后开始'],
                                    'keyword1' => ['value' => $course['name']],
                                    'keyword2' => ['value' => $stu['name']],
                                    'remark' => ['value' => '若因故无法参加，请及时联系老师'],
                                ];
                                write_log(json_encode($data));
                                $openid = $stu['fwhopenid'];
                                $res = $PublicsLogic->send_template_message($template_id, $page, $data, $openid);
                                write_log($res);
                            }
                            $tuisong_recode_data['user_id'] = $stu['id'];
                            $tuisong_recode_data['classtime_id'] = $ct['id'];
                            $tuisong_recode_data['create_time'] = time();
                            $is_insert = Db::name('tuisong_recode')->insertGetId($tuisong_recode_data);
                        }
                    }
                }
            }
        }
    }
}
